package com.alinesno.cloud.initializr.starter.gateway.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alinesno.cloud.common.facade.constants.FieldConstants;
import com.alinesno.cloud.common.facade.response.AjaxResult;
import com.alinesno.cloud.common.facade.wrapper.RpcWrapper;
import com.alinesno.cloud.initializr.starter.entity.InitializrDbListEntity;
import com.alinesno.cloud.initializr.starter.entity.ProjectInfoEntity;
import com.alinesno.cloud.initializr.starter.entity.StaticCountEntity;
import com.alinesno.cloud.initializr.starter.service.IBuildProjectInfoService;
import com.alinesno.cloud.initializr.starter.service.IInitializrDbListService;
import com.alinesno.cloud.initializr.starter.service.IStaticService;
import com.alinesno.cloud.rest.gateway.adapter.security.CurrentAccountJwt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;



/**
 * 应用界面管理
 * @author WeiXiaoJin
 * @since 2021年12月20日 下午6:56:54
 */
@RestController
@RequestMapping("/dashboard/common")
public class DashboardRest {

	private static final Logger log = LoggerFactory.getLogger(DashboardRest.class) ; 

	@Autowired
	private IBuildProjectInfoService projectService ; 
	
	@Autowired
	private IInitializrDbListService dbService ; 
	
	@Autowired
	private IStaticService staticService ; 

	/**
	 * 应用统计
	 * @return
	 */
	@GetMapping("/dashboardCount")
	public AjaxResult dashboardCount() {

		log.debug("应用统计.");
		
		AjaxResult r = AjaxResult.success() ; 
		
		String userId = CurrentAccountJwt.getUserId() ; 
		
		int appCount = projectService.count(new QueryWrapper<ProjectInfoEntity>().eq("operator_id", userId)) ; 
		int databaseCount = dbService.count(new QueryWrapper<InitializrDbListEntity>().eq("operator_id", userId)) ; 
		
		StaticCountEntity count = staticService.getById(1) ; //.getOne(new QueryWrapper<StaticCountEntity>().eq("operator_id", userId)) ; 
		if(count == null) {
			count = new StaticCountEntity() ; 
		}
	
		Map<String , Object> data = new HashMap<String , Object>() ; 
		data.put("appCount", appCount) ; 
		data.put("databaseCount", databaseCount) ; 
		
		data.put("genAppCount", count.getGenAppCount()) ; 
		data.put("genDataCount", count.getGenDatabaseCount()) ; 
		
		r.put("data", data) ;
		
		return r ; 
	}
	
	/**
	 * 应用列表
	 * @return
	 */
	@GetMapping("/getList")
	public AjaxResult getList() {
		
		log.debug("应用列表.");
	
		RpcWrapper<ProjectInfoEntity> wrapper = new RpcWrapper<>() ;
		wrapper.orderBy(FieldConstants.ADD_TIME , false) ;
		wrapper.eq("operator_id", CurrentAccountJwt.getUserId()) ; 
		
		List<ProjectInfoEntity> list = projectService.findTop(15, wrapper) ;
	
		return AjaxResult.success(list) ; 
	}
	
}
